//
//  DLRiskEvaluationChartView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/12/7.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLRiskEvaluationChartView.h"

@implementation DLRiskEvaluationChartView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.xValuesDataSource = [NSMutableArray array];
        self.setsDataSource = [NSMutableArray array];
        self.jsonDataSource = [NSMutableArray array];
        
        [self layoutSubControls];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void) layoutSubControls {
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(20, 20, self.width - 40, 40);
    titleLabel.textColor = kDLNormalBlackColor;
    titleLabel.font = kSystemFont(16);
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.backgroundColor = [UIColor whiteColor];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    titleLabel.tag = 666;
    [self addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    
    LineChartView *lineChartView = [[LineChartView alloc] initWithFrame:CGRectMake(titleLabel.left, titleLabel.bottom + 20, kScreenWidth - 40 - 30, kMachineChartsHeight)];
    [lineChartView setExtraOffsetsWithLeft:0 top:0 right:0 bottom:0];
    lineChartView.delegate = self;
    lineChartView.backgroundColor = [UIColor whiteColor];
    [self addSubview:lineChartView];
    self.lineChartView = lineChartView;
    
    lineChartView.autoScaleMinMaxEnabled = NO;
    lineChartView.gridBackgroundColor = [UIColor blackColor];
    lineChartView.gridBackgroundColor = [UIColor whiteColor];
    lineChartView.pinchZoomEnabled = YES;
    lineChartView.borderColor = [UIColor grayColor];
    lineChartView.borderLineWidth = 0.5;
    lineChartView.drawGridBackgroundEnabled = YES;
    lineChartView.drawBordersEnabled = YES;
    lineChartView.minOffset = 10;
    lineChartView.keepPositionOnRotation = YES;
    lineChartView.maxVisibleCount = 10;
    lineChartView.legend.enabled = NO;
    lineChartView.scaleYEnabled = NO;
    
    ChartXAxis *xAxis = lineChartView.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.drawGridLinesEnabled = YES;
    //    xAxis.granularityEnabled = YES;
    xAxis.labelCount = 4;
    xAxis.forceLabelsEnabled = NO;
    xAxis.labelTextColor = [UIColor whiteColor];
    xAxis.labelTextColor = kDLNormalBlackColor;
    xAxis.axisLineWidth = 0.5;
    xAxis.axisLineColor = [UIColor grayColor];
    xAxis.gridColor = [UIColor grayColor];
    
    
    ChartYAxis *leftAxis = lineChartView.leftAxis;
//    leftAxis.axisMinimum = 10.4;
//    leftAxis.axisMaximum = 10.7;
    leftAxis.labelCount = 6;
    leftAxis.drawGridLinesEnabled = YES;
    leftAxis.forceLabelsEnabled = NO;
    leftAxis.axisLineWidth = 0.5;
    leftAxis.axisLineColor = [UIColor grayColor];
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.labelTextColor = [UIColor whiteColor];
    leftAxis.labelTextColor = kDLNormalBlackColor;
    leftAxis.labelFont = kSystemFont(12);
    
    leftAxis.inverted = NO;
    leftAxis.zeroLineWidth = 0.5;
    leftAxis.drawZeroLineEnabled = YES;
    leftAxis.gridColor = [UIColor grayColor];
    self.leftAxis = leftAxis;
    
    //创建一个滑块对象
    
    UIView *bgView = [UIView new];
//    bgView.frame = CGRectMake(lineChartView.right + 10, lineChartView.top, lineChartView.height, 20);
    bgView.frame = CGRectMake(200, 220, lineChartView.height, 20);
    [self addSubview:bgView];
    
    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, bgView.width, bgView.height)];
    [bgView addSubview:slider];
    self.slider = slider;
    
    bgView.transform = CGAffineTransformMakeRotation(M_PI *1.5);
    
    //设置滑块最小值
    slider.minimumValue = 1;
    //设置滑块的最大值
    slider.maximumValue = 3;
    //用来设置是否实时显示数据
    slider.continuous = YES;
    
    
    //设置滑块的颜色,
    [slider setThumbTintColor:[UIColor grayColor]];
    //设置大值边颜色
    slider.maximumTrackTintColor = kColorRGB(236, 46, 136);
    //设置小值边颜色
    slider.minimumTrackTintColor = kDLBtnNormalBgColor;
    
    //给滑块加一个响应事件
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
//
}

//因为滑块是一个事件驱动型控件,所以可以加一个响应方法
-(void)sliderAction:(UISlider *)slider
{
//    NSLog(@"%.2f",slider.value);
    NSString *keyString = @"";
    NSArray *array = [NSArray array];
    if (slider.value >= 1 && slider.value <= 1.5) {
        array = self.jsonDataSource[0];
        keyString = @"compositeIndexGEM";
    } else if (slider.value <= 3 && slider.value >= 2.5) {
        array = self.jsonDataSource[2];
        keyString = @"compositeIndexShenzhen";
    } else {
        array = self.jsonDataSource[1];
        keyString = @"compositeIndexShanghai";
    }
    [self configLineChartData:array];
    if (self.userSelectResultBlock) {
        self.userSelectResultBlock(keyString, array);
    }
}

- (void) setChartDic:(NSDictionary *)chartDic {
    _chartDic = chartDic;
    
    NSDictionary *dic1 = chartDic[@"GRID0"];
    NSDictionary *dic = dic1[@"result"];
    
    [self.jsonDataSource removeAllObjects];
    [self.jsonDataSource addObject:dic[@"compositeIndexGEM"]];
    [self.jsonDataSource addObject:dic[@"compositeIndexShanghai"]];
    [self.jsonDataSource addObject:dic[@"compositeIndexShenzhen"]];
    
    [self configLineChartData:dic[@"compositeIndexGEM"]];
    
    
}
- (void) configLineChartData:(NSArray *)array {
    
    [self.xValuesDataSource removeAllObjects];
    [self.setsDataSource removeAllObjects];
    
    NSArray *kDataArray = [NSArray arrayWithArray:array];
    if (kDataArray.count > 0) {
        
        for (NSInteger i = 0; i < kDataArray.count; i++) {
            NSDictionary *dataDic = kDataArray[i];
            NSString *newTitleStr = [NSString stringWithFormat:@"%@", dataDic[@"tradeDate"]];
            NSString *setsValueStr = [NSString stringWithFormat:@"%@", dataDic[@"rate"]];
            
            [self.xValuesDataSource addObject:newTitleStr];
            [self.setsDataSource addObject:setsValueStr];
        }
    }
    ChartIndexAxisValueFormatter *formatter = [[ChartIndexAxisValueFormatter alloc] initWithValues:self.xValuesDataSource];
    [self.lineChartView.xAxis setValueFormatter:formatter];
    
    self.lineChartView.rightAxis.enabled = NO;
    
//    [self.lineChartView animateWithXAxisDuration:0.3];
    
    
    NSMutableArray *mutValues = [[NSMutableArray alloc] init];
    NSMutableArray *numberMutArr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.xValuesDataSource.count; i++) {
        NSString *valueStr = self.setsDataSource[i];
        double val =  valueStr.doubleValue;
        [mutValues addObject:[[ChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"noname,theredon'tneedanimage"]]];
        
        NSNumber *numberValue = [NSNumber numberWithDouble:val];
        [numberMutArr addObject:numberValue];
    }
    
    CGFloat maxPrice = [[numberMutArr valueForKeyPath:@"@max.floatValue"] floatValue];
    CGFloat minPrice = [[numberMutArr valueForKeyPath:@"@min.floatValue"] floatValue];
    NSLog(@"最大值：%.2f", maxPrice);
    NSLog(@"最小值：%.2f", minPrice);
    self.leftAxis.axisMinimum = minPrice;
    self.leftAxis.axisMaximum = maxPrice;
    
    self.leftAxis.axisMinimum = 0;
    self.leftAxis.axisMaximum = 0.3;
    
    
    LineChartDataSet *set1 = nil;
    if (_lineChartView.data.dataSetCount > 0) {
        set1 = (LineChartDataSet *)_lineChartView.data.dataSets[0];
        set1.values = mutValues;
        [_lineChartView.data notifyDataChanged];
        [_lineChartView notifyDataSetChanged];
    } else {
        
        set1 = [[LineChartDataSet alloc] initWithValues:mutValues label:@""];
//        [set1 setColor:[UIColor whiteColor]];
        [set1 setColor:kDLNormalBlackColor];
        set1.drawCirclesEnabled = NO;
        set1.circleHoleRadius = 1;
        set1.circleRadius = 1;
        set1.lineWidth = 1;
        set1.mode = LineChartModeCubicBezier;
        set1.drawCircleHoleEnabled = NO;
        set1.drawValuesEnabled = YES;
        //折线转折点的文字颜色
        set1.valueColors = @[[UIColor whiteColor]];
        set1.valueColors = @[kDLNormalBlackColor];
        set1.highlightEnabled = YES;
        set1.highlightColor = [UIColor orangeColor];
        //是否填充封闭区域以及封闭颜色
        set1.drawFilledEnabled = YES;
        [set1 setFillColor:kDLBtnNormalBgColor];
        
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
        _lineChartView.data = data;
    }
    
    if (self.userSelectResultBlock) {
        self.userSelectResultBlock(@"compositeIndexGEM", array);
    }
}
- (void) setTitleCount:(NSInteger)titleCount {
    _titleCount = titleCount;
    self.titleLabel.text = [NSString stringWithFormat:@"%ld  .请拖动右侧滑块，选择您偏好的收益曲线", self.titleCount + 1];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
